package com.symaster.common.entity;

import com.alibaba.fastjson.JSONObject;
import com.symaster.common.swing.element.SyzdPagination;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

/**
 * @author yinmiao
 * @version 2022-07-16 19:23
 */
public class PageResult<T> implements Serializable {
    private static final long serialVersionUID = -6849794470754667710L;

    /**
     * 总数
     */
    private Long totalElements;
    /**
     * 页面数量
     */
    private Integer totalPages;
    /**
     * 当前页面
     */
    private Integer number;
    /**
     * 页面数据数量
     */
    private Integer size;
    /**
     * 下一页
     */
    private Integer nextPage;
    /**
     * 内容
     */
    private List<T> content;

    public PageResult() {
        totalElements = 0L;
        number = 1;
        size = 1;
        nextPage = 1;
    }

    public PageResult(Long totalElements, Integer number, Integer size, List<T> content) {
        this.totalElements = totalElements == null ? (totalElements = 0L) : totalElements;
        this.number = number == null ? (number = 0) : number;
        this.size = size == null ? (size = 0) : size;
        this.content = content == null ? new LinkedList<>() : content;
        this.totalPages = (int) Math.ceil(totalElements / (double) (int) size);
        this.nextPage = number >= totalPages ? 1 : number + 1;
    }

    public static <R> PageResult<R> of(String s, Class<R> clazz) {
        JSONObject jsonObject = JSONObject.parseObject(s);
        PageResult<R> rPageResult = new PageResult<>(
                jsonObject.getLong("totalElements"),
                jsonObject.getInteger("number"),
                jsonObject.getInteger("size"),
                null
        );

        String s2 = jsonObject.getString("content");
        rPageResult.content = JSONObject.parseArray(s2, clazz);
        return rPageResult;
    }

    public SyzdPagination.Pagination toPagination() {
        SyzdPagination.Pagination pagination = new SyzdPagination.Pagination();
        pagination.setTotal((int) (long) totalElements);
        pagination.setPageSize(size);
        pagination.setPage(number);
        return pagination;
    }

    public boolean isEmpty() {
        return content == null || content.size() <= 0;
    }

    public Long getTotalElements() {
        return totalElements;
    }

    public void setTotalElements(Long totalElements) {
        this.totalElements = totalElements;
    }

    public Integer getNumber() {
        return number;
    }

    public void setNumber(Integer number) {
        this.number = number;
    }

    public Integer getSize() {
        return size;
    }

    public void setSize(Integer size) {
        this.size = size;
    }

    public Integer getTotalPages() {
        return totalPages;
    }

    public void setTotalPages(Integer totalPages) {
        this.totalPages = totalPages;
    }

    public Integer getNextPage() {
        return nextPage;
    }

    public void setNextPage(Integer nextPage) {
        this.nextPage = nextPage;
    }

    public List<T> getContent() {
        return content;
    }

    public void setContent(List<T> content) {
        this.content = content;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        PageResult<?> that = (PageResult<?>) o;
        return Objects.equals(totalElements, that.totalElements) && Objects.equals(number, that.number) && Objects.equals(size, that.size) && Objects.equals(totalPages, that.totalPages) && Objects.equals(nextPage, that.nextPage) && Objects.equals(content, that.content);
    }

    @Override
    public int hashCode() {
        return Objects.hash(totalElements, number, size, totalPages, nextPage, content);
    }

    @Override
    public String toString() {
        return "PageResult{" +
                "total=" + totalElements +
                ", pageNum=" + number +
                ", pageSize=" + size +
                ", pages=" + totalPages +
                ", nextPage=" + nextPage +
                ", list=" + content +
                '}';
    }
}
